# encoding: utf-8
module SpendsHelper
	
	#
	# Exibe os atributos de um objeto.
	# Por padrão, todos os atributos são exibidos,
	# exceto o 'id', 'created_at' e 'updated_at'
	#
	# - options:
	#   - model => objeto a ser exibido
	#		- except => array de atributos (string) que não devem ser exibidos
	#		- only => array de atributos (string) que devem ser exibidos
	#		- on =>
	#		- relations => informa o valor do atributo de um relacionamento que deverá ser exibido
	def show_model(model, options={})
		return if model.nil?
		
		all = options[:all] || false
		except = ["id", "created_at", "updated_at"].concat(options[:except] || [])
		only = options[:only] || model.attribute_names
		relations = options[:relations] || {}
		on_hash = options[:on] || {}
		on_value = nil

		(only - except).collect do |att|
			#TODO refatorar
			if (att.end_with?("_id"))
				rel_name = att.delete("_id")
				rel_value = relations[rel_name]
				
				unless (rel_value.nil?)
					val = model.send(rel_name).send(rel_value)
				end
			else
				on_value = on_hash[att]
				val = model.instance_eval((on_value.nil?) ? att : on_value)
			end

			#TODO refatorar tb
			if val.is_a?(BigDecimal)
				val = number_to_currency(val)
			elsif val.is_a?(Date)
				val = val.strftime("%d/%m/%Y")
			elsif val.is_a?(TrueClass)
				val = "Sim"
			elsif val.is_a?(FalseClass)
				val = "Não"
			end
			
			unless (val.nil?)
				content_tag(:p) do
					concat(content_tag(:b, "#{model.class.human_attribute_name(att)}: "))
					concat(content_tag(:span, val))
				end
			end
		end.join.html_safe
	end

	#
	#
	def icon_search
		bootstrap_icon("search")
	end

	#
	#
	def icon_edit
		bootstrap_icon("edit")
	end

	#
	#
	def icon_remove
		bootstrap_icon("remove")
	end

	#
	#
	def show_link(object)
		path = polymorphic_path(object)
		link_to(icon_search, path, :class => "btn", :title => "Exibir")
	end

	#
	#
	def edit_link(object)
		path = edit_polymorphic_path(object)
		link_to(icon_edit, path, :class => "btn", :title => "Alterar")
	end

	#
	#
	def destroy_link(object)
		path = polymorphic_path(object)

		link_to(icon_remove, path,
			:method => :delete, :confirm => "Confirma a exclusão?",
			:class => "btn",  :title => "Excluir")
	end

	#
	#
	def delete_link(object)
		path = polymorphic_path(object, :action => "delete")

		link_to(icon_remove, path, :class => "btn",  :title => "Excluir")
	end

	#
	#
	def bootstrap_alert(type = nil, &block)
		alert = (!type.nil? and !type.empty?) ? "alert-#{type}" : ""
	
		content_tag(:div, :class => "alert #{alert}") do
			capture(&block)
		end
	end

	#
	#
	def flash_alerts
		return unless flash[:notice] or flash[:alert]
	
		content_tag(:div, :class => "row-fluid") do
			content_tag(:div, :class => "span12") do
				concat(flash_notice) if flash[:notice]
				concat(flash_alert) if flash[:alert]
			end
		end
	end

	#
	#
	def errors_for(model)
		return if model.nil? or model.errors.empty?

		content_tag(:div, :class => "row-fluid") do
			content_tag(:div, :class => "span6") do
				content_tag(:div, :class => "alert alert-error") do
					model.errors.full_messages.each do |m|
						concat(content_tag(:p, m))
					end
				end
			end
		end
	end
	
	private

	#
	#
	def bootstrap_icon(type)
		content_tag(:i, nil, :class => "icon-#{type}")
	end
	
	#
	#
	def flash_notice
		content_tag(:div, :class => "alert alert-success model-alerts") do
			concat(content_tag(:p, flash[:notice]))
		end
	end

	#
	#
	def flash_alert
		content_tag(:div, :class => "alert alert-error model-alerts") do
			concat(content_tag(:p, flash[:alert], :class => "flash"))
		end
	end
end
